﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TemaAdministration.Models;

namespace TemaAdministration
{
    public class CustomRoleProvider : System.Web.Security.RoleProvider
    {
        #region Overrides of RoleProvider

        public override bool IsUserInRole(string username, string roleName)
        {
            var db = new F11ITNET2Gr7Entities();
            var user = db.GPSysUsers.Single(u => u.UserName == username);

            if (user == null)
                return false;
            
            var roles = GetRolesForUser(username);
            return roles.Contains(roleName);
        }

        public override string[] GetRolesForUser(string username)
        {
            var db = new F11ITNET2Gr7Entities();
            var user = db.GPSysUsers.Single(u => u.UserName == username);

            if (user == null) return null;

            string[] roles = new string[user.GPSysRoles.Count + 1];
            int idx = 0;
            foreach (GPSysRole role in user.GPSysRoles)
                roles[++idx] = role.Name;
            return roles;
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        #endregion
    }
}